﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using Zeus.Thunder.Core;

namespace SharpTrainer.Other
{
    class ZipFileTest : ITestCase
    {
        public void Run()
        {
            //string dir = AppDomain.CurrentDomain.BaseDirectory;
            //string zipFilePath = "D://ZipTest//zipfile.zip";

            string zipFilePath = Path.Combine(Path.GetFullPath("..\\..\\"), "Other\\zipfile.zip");

            string fileName = "1.txt";
            bool hasFile = HasFileInZip(fileName, zipFilePath);
            Console.WriteLine("Zip File: {0} has file {1}: {2}", zipFilePath, fileName, hasFile);

            fileName = "5.txt";
            hasFile = HasFileInZip(fileName, zipFilePath);
            Console.WriteLine("Zip File: {0} has file {1}: {2}", zipFilePath, fileName, hasFile);
        }

        private bool HasFileInZip(string fileName, string zipFile)
        {
            using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFile)))
            {
                ZipEntry theEntry;

                while ((theEntry = s.GetNextEntry()) != null)
                {
                    if (fileName == theEntry.Name)
                    {
                        return true;
                    }
                }

                return false;
            }
        }
    }
}

